package com.fengye.jpa.converter.service;

import com.fengye.jpa.converter.model.many2many.Author;
import com.fengye.jpa.converter.model.many2many.Book;
import com.fengye.jpa.converter.respository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Optional;
import java.util.Set;

/**
 * @Author : sky
 * @Email : <mailto:fengyexjtu@126.com>
 * @Date: 2021/04/12 21:31
 * @Description:
 */

@Service
public class BookService {

    @Autowired
    BookRepository bookRepository;
    @Transactional
    public Book getBookById(Long id){
        Optional<Book> optionalBook = Optional.ofNullable(bookRepository.findOne(id));
        Book book = optionalBook.orElseThrow(() -> new RuntimeException("not found"));
        return book;
    }

    @Transactional
    public Set<Author> getAuthor(Book book) {
//        Book book = bookService.getBookById(3L);
//        Book book1 = book.orElseGet(Book::new);
//        System.out.println(book.getAuthors());
        return book.getAuthors();
    }
}
